<template>
  <div>
    <el-dialog
      v-model="visibleModel"
      title="销售订单"
      width="900"
      style="height:90%;display:flex;flex-direction:column;justify-content:space-between;"
      align-center
      draggable
      overflow
      :before-close="handleClose"
      :close-on-click-modal="false"
    >
      <template #header>
        <div>
          <span v-if="props.dialogType === 'add'">创建销售订单</span>
          <span v-if="props.dialogType === 'edit'">编辑销售订单</span>
          <!-- <div class="btn-list" v-if="props.dialogType === 'edit'">
            <el-button size="small" :icon="DocumentCopy">复制</el-button>
            <el-button size="small" icon="delete">删除</el-button>
            <el-button size="small" icon="upload" >导出</el-button>
          </div> -->
        </div>
      </template>
      <el-form :model="orderStore.order" label-position="top" ref="orderRef" :rules="orderStore.orderRules">
        <div class="form-container">
          <el-form-item label="客户" prop="clientName">
            <el-autocomplete
              v-model="orderStore.order.clientName"
              :fetch-suggestions="querySearch"
              :trigger-on-focus="false"
              clearable
              style="width: 250px; height: 32px;"
              @select="handleClientSelect"
            />
          </el-form-item>
          <el-form-item label="下单日期" prop="orderDt">
            <el-date-picker
              v-model="orderStore.order.orderDt"
              style="width: 250px;"
              type="date"
              placeholder="Pick a date"
              clearable
              value-format="YYYY-MM-DD"
              :disabled-date="disabledDate"
            />
          </el-form-item>
          <el-form-item label="要求送达日期" prop="scheduledDt">
            <el-date-picker
              v-model="orderStore.order.scheduledDt"
              style="width: 250px;"
              type="date"
              placeholder="Pick a date111"
              clearable
              value-format="YYYY-MM-DD"
              :disabled-date="disabledDate"
            />
          </el-form-item>
        </div>
        <div v-for="(item, index) in orderStore.order.skuList" :key="item.no" class="sku-card" ref="skuCardRefs">
        <el-button
          type="danger" text plain size="small" 
          style="float: right;" 
          :icon="item.operationType === 'add' ? 'Delete' : 'CloseBold'"
          @click="handleDelete(index)"
          v-if="props.dialogType === 'add' && orderStore.order.skuList.length > 1 || props.dialogType === 'edit' && item.orderState === '待送货'"
        />
        <el-form-item style="font-weight: bold;">
          <template #label>
            <div style="display: flex; align-items: center;">
              <span>商品{{ index + 1 }}</span>
              <el-tag
                v-if="props.dialogType === 'edit'"
                style="margin-left: 10px; height: 22px; line-height: 22px;"
                :type="handleOrderState(item.orderState)"
              >
                {{ item.orderState }}: 
                {{ item.actualDeliver }}
                 / 
                {{ isPositiveNumber(item.quantity) ? item.quantity : "-" }}
              </el-tag>
            </div>
          </template>
          <el-button 
            style="width: 250px;"
            @click="handleSkuUsual(index)"
            :disabled="!isSkuEdited[index]"
          >
            选择常用数据
          </el-button>
        </el-form-item>
        <div class="form-container">
          <el-form-item label="商品名称" :prop="`skuList.${index}.skuName`" :rules="orderStore.orderRules.skuName">
            <el-input 
              v-model="item.skuName"
              style="width: 175px;"
              placeholder="暂无数据"
              :disabled="!isSkuEdited[index]"
            />
          </el-form-item>
          <el-form-item label="牌号/款式" :prop="`skuList.${index}.skuBrand`" :rules="orderStore.orderRules.skuBrand">
            <el-input 
              v-model="item.skuBrand"
              style="width: 175px;"
              placeholder="暂无数据"
              :disabled="!isSkuEdited[index]"
              @input="item.internalBrand = item.skuBrand"
            />
          </el-form-item>
          <el-form-item label="内部牌号" :prop="`skuList.${index}.internalBrand`" :rules="orderStore.orderRules.internalBrand">
            <el-input 
              v-model="item.internalBrand"
              style="width: 175px;"
              placeholder="暂无数据"
              :disabled="!isSkuEdited[index]"
            />
          </el-form-item>
          <el-form-item label="商品类型" :prop="`skuList.${index}.skuType`" :rules="orderStore.orderRules.skuType">
            <el-select 
              style="width: 175px;"
              v-model="item.skuType"
              @change="handleSelectSkuType(index)"
              placeholder="暂无数据"
              :disabled="!isSkuEdited[index]"
            >
              <el-option v-for="(item, index) in CONFIG.SKU_SPEC.SPEC_TYPE" :key="index" :label="item.label" :value="item.value" style="height: 29px;line-height: 29px;"/>
            </el-select>
          </el-form-item>
        </div> 
        <div class="form-container">
          <el-form-item 
            label="规格参数-长 (mm)" :prop="`skuList.${index}.specLength`" 
            v-if="CONFIG.SKU_SPEC.SPEC_LENGTH.includes(item.skuType)"
            :rules="orderStore.orderRules.specLength"
          >
            <el-input
              v-model="item.specLength"
              style="width: 175px;"
              placeholder="暂无数据"
              @input="handleSpecInput(index)"
              :disabled="!isSkuEdited[index]"
            />
          </el-form-item>
          <el-form-item 
            label="规格参数-宽 (mm)" :prop="`skuList.${index}.specWidth`" 
            v-if="CONFIG.SKU_SPEC.SPEC_WIDTH.includes(item.skuType)"
            :rules="orderStore.orderRules.specWidth"
          >
            <el-input
              v-model="item.specWidth"
              style="width: 175px;"
              placeholder="暂无数据"
              @input="handleSpecInput(index)"
              :disabled="!isSkuEdited[index]"
            />
          </el-form-item>
          <el-form-item 
            label="规格参数-厚 (mm)" :prop="`skuList.${index}.specThickness`" 
            v-if="CONFIG.SKU_SPEC.SPEC_THICKNESS.includes(item.skuType)"
            :rules="orderStore.orderRules.specThickness"
          >
            <el-input
              v-model="item.specThickness"
              style="width: 175px;"
              placeholder="暂无数据"
              @input="handleSpecInput(index)"
              :disabled="!isSkuEdited[index]"
            />
          </el-form-item>
          <el-form-item 
            label="规格参数-直/外径 (mm)" :prop="`skuList.${index}.specDiameter`" 
            v-if="CONFIG.SKU_SPEC.SPEC_DIAMETER.includes(item.skuType)"
            :rules="orderStore.orderRules.specDiameter"
          >
            <el-input
              v-model="item.specDiameter"
              style="width: 175px;"
              placeholder="暂无数据"
              @input="handleSpecInput(index)"
              :disabled="!isSkuEdited[index]"
            />
          </el-form-item>
          <el-form-item 
            label="规格参数-内径 (mm)" :prop="`skuList.${index}.specInnerD`" 
            v-if="CONFIG.SKU_SPEC.SPEC_INNER_D.includes(item.skuType)"
            :rules="orderStore.orderRules.specInnerD"
          >
            <el-input
              v-model="item.specInnerD"
              style="width: 175px;"
              placeholder="暂无数据"
              @input="handleSpecInput(index)"
              :disabled="!isSkuEdited[index]"
            />
          </el-form-item>
          <el-form-item 
            label="规格参数-偏心直径 (mm)" :prop="`skuList.${index}.specEccentricD`" 
            v-if="CONFIG.SKU_SPEC.SPEC_ECCENTRIC_D.includes(item.skuType)"
            :rules="orderStore.orderRules.specEccentricD"
          >
            <el-input
              v-model="item.specEccentricD"
              style="width: 175px;"
              placeholder="暂无数据"
              @input="handleSpecInput(index)"
              :disabled="!isSkuEdited[index]"
            />
          </el-form-item>
          <!-- <el-form-item 
            label="规格参数-其他" :prop="`skuList.${index}.specOtherSpec`" 
            v-if="CONFIG.SKU_SPEC.SPEC_OTHER.includes(item.skuType)"
            :rules="orderStore.orderRules.specOtherSpec"
          >
            <el-input
              v-model="item.specOtherSpec"
              style="width: 175px;"
              placeholder="暂无数据"
              @input="handleSpecInput(index)"
              :disabled="!isSkuEdited[index]"
            />
          </el-form-item> -->

          <el-form-item 
            v-if="CONFIG.SKU_SPEC.SPEC_OTHER.includes(item.skuType)"
            v-for="itemSpec in orderStore.allSpecification"
            :key="itemSpec.prefix"
            :label="`规格参数-${itemSpec.displayName}`"
            :prop="`skuList.${index}.otherSpecs.${itemSpec.prefix}`"
            :rules="['int', 'double'].includes(itemSpec.dataType) && orderStore.orderRules.specOtherSpec"
          >
            <el-input
              v-model="item.otherSpecs[itemSpec.prefix]"
              style="width: 175px;"
              @input="handleSpecInput(index)"
            >
              <template #prepend>
                <span>{{ itemSpec.prefix }}</span>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="规格型号" :prop="`skuList.${index}.specification`" :rules="orderStore.orderRules.specification">
            <el-input 
              v-model="item.specification"
              style="width: 175px;"
              placeholder="暂无数据"
              disabled
            />
          </el-form-item>
        </div>
        <div class="form-container">
          <!-- <el-form-item label="库存量">
            <el-input
              style="width: 175px;"
              placeholder="暂无数据"
              disabled
            />
          </el-form-item> -->
          <el-form-item label="订单量" :prop="`skuList.${index}.quantity`" :rules="orderStore.orderRules.quantity">
            <el-input 
              v-model="item.quantity"
              style="width: 175px;"
              placeholder="暂无数据"
              @input="handleUpdateWeight(index)"
              :disabled="!isSkuEdited[index]"
            />
          </el-form-item>
          <el-form-item label="订货单位" :prop="`skuList.${index}.unit`" :rules="orderStore.orderRules.unit">
            <el-select 
              v-model="item.unit" 
              style="width: 175px;"
              @change="handleSelect(index)"
              placeholder="暂无数据"
              :disabled="['其他','镜','环','针'].includes(item.skuType) || !isSkuEdited[index]"
            >
              <el-option label="pcs" value="pcs" />
              <el-option label="kg" value="kg" />
            </el-select>
          </el-form-item>
          <el-form-item label="是否按数量 (pcs) 计费" :prop="`skuList.${index}.byQuantity`" :rules="orderStore.orderRules.byQuantity">
            <el-select 
              v-model="item.byQuantity" 
              style="width: 175px;" 
              placeholder="暂无数据" 
              :disabled="['其他','镜','环','针'].includes(item.skuType) || !isSkuEdited[index]"
              @change="handleSelectByQuantity(item)"
            >
              <el-option v-if="item.unit !== 'kg'" label="是" value="1" />
              <el-option label="否" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item 
            :label="item.isEstimate === 1 ? '估算重量 (kg)' : '订货重量 (kg)'" 
            :prop="`skuList.${index}.weight`" :rules="orderStore.orderRules.weight"
          >
            <el-input
              v-model="item.weight"
              :disabled="item.isEstimate === 1 || item.unit === 'kg'"
              style="width: 175px;"
              placeholder="N/A" 
              @input="handleUpdateQuantity(index)"
            />
          </el-form-item>
          <el-form-item label="单价" :prop="`skuList.${index}.unitPrice`" :rules="orderStore.orderRules.unitPrice">
            <el-input
              v-model="item.unitPrice"
              style="width: 175px;"
              placeholder="暂无数据"
              :disabled="!isSkuEdited[index]"
            />
          </el-form-item>
          <el-form-item 
            :label="item.isEstimate === 1 ? '估算金额' : '金额'" 
            :prop="`skuList.${index}.amount`" :rules="orderStore.orderRules.amount"
          >
            <el-input 
              v-model="item.amount"
              style="width: 175px;"
              placeholder="暂无数据"
              disabled
            />
          </el-form-item>
          <el-form-item label="估算订货重量与金额" prop="isEstimate" style="width: 175px;">
            <el-checkbox v-model="item.isEstimate"
              :true-value="1"
              :false-value="0"
              style="height: 32px;"
              label="是 / 否"
              size="large"
              :disabled="!(item.unit === 'pcs' && item.byQuantity === '0')"
              @change="handleEstimate(item)"
            />
          </el-form-item>
        </div>
        </div>
        <skuDialog ref="skuRef" v-model="dialogVisibleSku" :index="skuIndex" />
        <el-button
          icon="plus"
          type="primary"
          plain
          style="margin: -10px 0 10px; width: 250px;"
          @click="handleAddSku"
        >
          添加商品
        </el-button>
        <el-form-item label="备注" prop="remark">
          <el-input 
            v-model="orderStore.order.remark"
            style="width: 90%;"
            placeholder="暂无数据"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button 
            type="primary" style="min-width: 100px;"
            :disabled="isSubmitting"
            @click="handleSubmit(orderRef)">
              提交
          </el-button>
          <el-button plain style="width: 100px;" @click="handleCancel">
            取消
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, watchEffect, watch, h } from 'vue';
import { useOrderStore } from '@/stores/sale/order'
import skuDialog from '@/pages/home/views/sale/order/components/skuDialog.vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import { CloseBold, DocumentCopy } from '@element-plus/icons-vue'
import { insertOrder, updateOrder, getOrderSuggestions, getOrEstimationWeight, cancelOrder } from '@/pages/home/views/sale/api/order'
import { getStock } from '@/pages/home/views/sale/api/delivery'
import * as CONFIG from '@/pages/home/views/sale/config/order'
import { ElMessage, ElMessageBox } from 'element-plus'

const orderStore = useOrderStore()
const dialogVisibleSku = ref(false)
const orderRef = ref<FormInstance>()
const visibleModel = defineModel()
const skuStock = ref([])
const isSubmitting = ref(false)
const skuRef = ref()
const skuIndex = ref(0)
const skuCardRefs = ref([])
const isSkuEdited = ref([])

const props = defineProps<{
  dialogType: string
  getTableData: () => Promise<void>
}>()


const handleAddSku = () => {
  orderStore.addSku()
}
const handleClose = (done) => {
  // visibleModel.value = false
  orderStore.resetOrder(orderRef)
  console.log("order reset:", JSON.parse(JSON.stringify(orderStore.order)))
  done()
}
const handleCancel = () => {
  visibleModel.value = false
  orderStore.resetOrder(orderRef)
}
const handleSubmit = async (formRef) => {
  isSubmitting.value = true
  if (!formRef) return
  await formRef.validate(async(valid, fields) => {
        if (valid) {
          console.log('submit!')
          const orderCopy = JSON.parse(JSON.stringify(orderStore.order))
          orderCopy.skuList.forEach((sku) => {
            sku.quantity = Number(sku.quantity)
            sku.weight = Number(sku.weight)
            sku.unitPrice = Number(sku.unitPrice)
            delete sku.skuType
            delete sku.specLength
            delete sku.specWidth
            delete sku.specThickness
            delete sku.specDiameter
            delete sku.specInnerD
            delete sku.specOtherSpec
            delete sku.specEccentricD
            delete sku.operationType
          })
          console.log("submit order:", orderCopy)
          switch(props.dialogType){
            case 'edit': 
              {
                try {
                  const res = await updateOrder(orderCopy)
                  if(!res.data.success){
                    ElMessage.error(`${res.data.errorCode}: ${res.data.errorMsg}`)
                    isSubmitting.value = false
                    return
                  }
                }
                catch(error){
                  ElMessage.error(error)
                  isSubmitting.value = false
                  return
                }
              }
              break
            case 'add':
              {
                try {
                  const res = await insertOrder(orderCopy)
                  if(!res.data.success){
                    ElMessage.error(`${res.data.errorCode}: ${res.data.errorMsg}`)
                    isSubmitting.value = false
                    return
                  }
                }
                catch(error){
                  ElMessage.error(error)
                  isSubmitting.value = false
                  return
                }
              }
              break
          }
          await props.getTableData()
          visibleModel.value = false
          orderStore.resetOrder(orderRef)
          isSubmitting.value = false
        } else {
          console.log('error submit!')
          isSubmitting.value = false
        }
      })
}
const handleSelect = (index) => {
  if(orderStore.order.skuList[index].unit === "kg"){
    orderStore.order.skuList[index].byQuantity = "0"
    if(orderStore.order.skuList[index].quantity){
      handleUpdateWeight(index)
    }
    else if(orderStore.order.skuList[index].weight){
      handleUpdateQuantity(index)
    }
  }
  else orderStore.order.skuList[index].weight = null
}
const handleSelectSkuType = (index) => {
  orderStore.order.skuList[index] = {
    ...orderStore.order.skuList[index],
    specLength: null,
    specWidth: null,
    specThickness: null,
    specDiameter: null,
    specInnerD: null,
    specOtherSpec: null,
    specification: "",
  }
  if(["其他","镜","环","针"].includes(orderStore.order.skuList[index].skuType)){
    orderStore.order.skuList[index].byQuantity = "1"
    orderStore.order.skuList[index].unit = "pcs"
  }
}
const handleSpecInput = (index) => {
  let skuIndex = orderStore.order.skuList[index]
  switch(skuIndex.skuType){
    case '棒':
      if(isPositiveNumber([skuIndex.specDiameter, skuIndex.specLength]))
        skuIndex.specification = `D${skuIndex.specDiameter}*L${skuIndex.specLength}`
      else skuIndex.specification = ""
      break
    case '管':
      if(isPositiveNumber([skuIndex.specDiameter, skuIndex.specInnerD, skuIndex.specLength]))
        skuIndex.specification = `D${skuIndex.specDiameter}*d${skuIndex.specInnerD}*L${skuIndex.specLength}`
      else skuIndex.specification = ""
      break
    case '板':
      if(isPositiveNumber([skuIndex.specLength, skuIndex.specWidth, skuIndex.specThickness]))
        skuIndex.specification = `L${skuIndex.specLength}*W${skuIndex.specWidth}*T${skuIndex.specThickness}`
      else skuIndex.specification = ""
      break
    case '线':
      if(isPositiveNumber([skuIndex.specDiameter]))
        skuIndex.specification = `D${skuIndex.specDiameter}`
      else skuIndex.specification = ""
      break
    case '带':
      if(isPositiveNumber([skuIndex.specWidth, skuIndex.specThickness]))
        skuIndex.specification = `W${skuIndex.specWidth}*T${skuIndex.specThickness}`
      else skuIndex.specification = ""
      break
    case '镜':
      if(isPositiveNumber([skuIndex.specDiameter, skuIndex.specThickness]))
        skuIndex.specification = `D${skuIndex.specDiameter}*T${skuIndex.specThickness}`
      else skuIndex.specification = ""
      break
    case '环':
      if(isPositiveNumber([skuIndex.specDiameter, skuIndex.specInnerD]))
        skuIndex.specification = `D${skuIndex.specDiameter}*d${skuIndex.specInnerD}`
      else skuIndex.specification = ""
      break
    case '针':
      if(isPositiveNumber([skuIndex.specDiameter, skuIndex.specLength, skuIndex.specEccentricD]))
        skuIndex.specification = `D${skuIndex.specDiameter}*L${skuIndex.specLength}*Φ${skuIndex.specEccentricD}`
      else skuIndex.specification = ""
      break
    case '其他':
      for(let key in skuIndex.otherSpecs){
        if(!skuIndex.otherSpecs[key]){
          delete skuIndex.otherSpecs[key]
        }
      }
      if(isPositiveNumber(skuIndex.otherSpecs) && Object.keys(skuIndex.otherSpecs).length > 0){
        skuIndex.specification = Object.keys(skuIndex.otherSpecs).map(key => {
          return `${key}${skuIndex.otherSpecs[key]}`
        }).join("*")
      }
      else skuIndex.specification = ""
      break
  }
}
const handleDelete = async (index) => {
  if(!orderStore.order.skuList[index].operationType){
    try {
      await ElMessageBox(
        {
          title: '提示',
          message: h('div', null, [
            h('span', null, `是否确定取消订单 `),
            h('strong', null, `商品${index + 1}：${orderStore.order.skuList[index].skuName}`),
            h('span', null, ` ？`),
            h('br'),
            h('span', null, `此操作不可撤回 。`,),
          ]
          ),
          confirmButtonText: '确定',
          showCancelButton: true,
          cancelButtonText: '取消',
        }
      )
      const res = await cancelOrder({
        orderId: orderStore.order.orderId,
        skuId: orderStore.order.skuList[index].skuId,
      })
      if(!res.data.success){
        ElMessage.error(`${res.data.errorCode}: ${res.data.errorMsg}`)
      }
      else {
        orderStore.order.skuList[index].orderState = "已取消"
      }
    }
    catch (error) {}
  }
  else {
    orderStore.order.skuList.splice(index, 1)
  }
}
const querySearch = async (queryString, cb) => {
  try {
    const suggestions = await getOrderSuggestions({
      callWord: queryString
    })
    if(suggestions.data.length === 0){
      cb([])
    }
    else {
      cb(suggestions.data.map(suggestion => ({ value: suggestion })))
    }
  }
  catch (error) {
    cb([])
  }
}
const handleClientSelect = (item) => {
  orderStore.order.clientName = item.value
}
const disabledDate = (time: Date) => {
  return time.getTime() < Date.now() - 8.64e7
}
// const handleStock = async () => {
//   for(let i = 0; i < orderStore.order.skuList.length; i++){
//     try {
//       const stock = await getStock(orderStore.order.skuList[i].skuId)
//       skuStock.value.push(stock.data)
//     }
//     catch {
//       console.log("未获取到库存数据")
//     } 
//   }
// }
const handleSkuUsual = async (index) => {
  dialogVisibleSku.value = true
  skuIndex.value = index
  await skuRef.value.getUsualSkuTableData()
}
const handleUpdateWeight = (index) => {
  if(orderStore.order.skuList[index].unit === "kg"){
    orderStore.order.skuList[index].weight = orderStore.order.skuList[index].quantity
  }
}
const handleUpdateQuantity = (index) => {
  if(orderStore.order.skuList[index].unit === "kg"){
    orderStore.order.skuList[index].quantity = orderStore.order.skuList[index].weight
  }
}
const handleEstimate = async (item) => {
    if(item.isEstimate){
      if(!item.skuName || !item.skuBrand){
        ElMessage.error("估算前，请确保填写正确的商品名称与牌号")
        item.isEstimate = 0
        return
      }
      else if(!item.specification){
        ElMessage.error("估算前，请确保填写正确的规格型号")
        item.isEstimate = 0
        return
      }
      else if(!item.quantity || !Number.isFinite(Number(item.quantity)) || Number(item.quantity) < 0){
        ElMessage.error("估算前，请确保填写正确的订单量")
        item.isEstimate = 0
        return
      }
      try {
        item.weight = "正在估算重量中..."
        const estimatedWeight = await getOrEstimationWeight({
          skuName: item.skuName,
          skuBrand: item.skuBrand,
          spec: item.specification,
          quantity: item.quantity
        })
        if(estimatedWeight.data?.success === false){
          ElMessage.error(`重量估算失败，${estimatedWeight.data.errorMsg}`)
          item.weight = null
          return
        }
        item.weight = Number(estimatedWeight.data.toFixed(2))
        orderStore.order.remark += "（本订单金额按理论重量计算，最终以实际生产称重为准）"
      }
      catch (error) {
        ElMessage.error("重量估算失败，请稍后重试")
        item.weight = null
        item.isEstimate = 0
        return
      }
    }
    else {
      item.weight = null
      orderStore.order.remark = orderStore.order.remark.replace("（本订单金额按理论重量计算，最终以实际生产称重为准）", "")
    }
    
}
const handleSelectByQuantity = (item) => {
  if(!(item.unit === 'pcs' && item.byQuantity === '0')){
    item.isEstimate = 0
    item.weight = null
  }
}
const handleOrderState = (row) => {
  switch (row) {
    case "已完成":
      return "success"
    case "部分送货":
      return "warning"
    case "待送货":
      return "danger"
    default:
      return "info"
  }
}
const isPositiveNumber = (number) => {
  if(Array.isArray(number)){
    for(let i = 0; i < number.length; i++){
      if(!number[i] || !Number.isFinite(Number(number[i])) || Number(number[i]) < 0) return false
    }
  }
  else if(typeof number === "object"){
    for(let key in number){
      if(orderStore.allSpecification.find(item => item.prefix === key && ['int', 'double'].includes(item.dataType))){
        if(!number[key] || !Number.isFinite(Number(number[key])) || Number(number[key]) < 0) return false
      }
    }
  }
  else if(!number || !Number.isFinite(Number(number)) || Number(number) < 0) return false
  return true
}

watchEffect(() => {
  orderStore.order.skuList.forEach((item, index) => {
    if(props.dialogType === "add") isSkuEdited.value[index] = true
    else if(props.dialogType === "edit" && item.orderState === "待送货") isSkuEdited.value[index] = true
    else isSkuEdited.value[index] = false
  })
})
</script>

<style scoped>
:deep(.el-dialog__body) {
    overflow: auto;
    flex: 1;
    --el-disabled-text-color: var(--el-text-color-regular);
}
:deep(.el-dialog__footer) {
    align-self: start;
}
.btn-list {
  height:35px;
  border-top: 1px solid #eee;
  border-bottom: 1px solid #eee;
  display: flex;
  align-items: center;
  margin-top: 10px;
  gap: 10px;
}
.form-container {
  display: flex;
  flex-flow: row wrap;
  column-gap: 30px;
}
.sku-card {
  width: 95%;
  margin-bottom: 20px;
  border: 1px solid #e1e3e5;
  border-radius: 4px;
  padding: 5px;
}
</style>